package com.neusoft.yuejian12.yuejian_12.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.TrainDetailActivity;
import com.neusoft.yuejian12.yuejian_12.javabean.TrainBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/10/8.
 */

public class SearchTrainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContex;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public SearchTrainAdapter(Context mContext, List mDataList) {
        this.mContex = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

//    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.activity_search_train, parent, false);
        return new ViewHolder(v);
    }
//
//    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder Trainholder, int position) {

        final TrainBean searchtrain = (TrainBean) mDataList.get(position);
        if (null == searchtrain)
            return;
        ViewHolder viewHolder = (ViewHolder) Trainholder;
        viewHolder.t_name.setText(searchtrain.getT_name());
        String t_picture = "http://10.0.2.2:888/yuejian14/Public/Admin/trainpicture/" + searchtrain.getT_picture();
        //System.out.print("pic");
        if (!t_picture.equals("")) {
            Picasso.with(mContex).load(t_picture).error(R.drawable.ic_add).into(viewHolder.t_picture);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("t_id", searchtrain.getT_id());
                bundle.putString("t_name", searchtrain.getT_name());
                bundle.putInt("t_price", searchtrain.getT_price());
                bundle.putString("t_address", searchtrain.getT_address());
                bundle.putString("t_time", searchtrain.getT_time());
                bundle.putString("t_picture", searchtrain.getT_picture());

                bundle.putString("t_phone", searchtrain.getT_phone());
                Intent intent = new Intent(mContex, TrainDetailActivity.class);
                intent.putExtras(bundle);
                mContex.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView t_picture;
        TextView t_name;
        //  ImageView search_train;

        public ViewHolder(View itemView) {
            super(itemView);
            t_name = (TextView) itemView.findViewById(R.id.t_name);
            t_picture = (ImageView) itemView.findViewById(R.id.t_picture);

        }

    }
}